package com.example.demo.sys.service.impl;

import com.example.demo.core.utils.BeanConvertor;
import com.example.demo.sys.dao.DeptRepository;
import com.example.demo.sys.entity.Department;
import com.example.demo.sys.service.DeptService;
import com.example.demo.sys.service.bo.DepartmentBO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptRepository deptRepository;


    @Override
    public List<DepartmentBO> getAllDepartments() {
        Iterable<Department> res = deptRepository.findAll();
        List<DepartmentBO> list = new ArrayList<>();
        res.forEach(item -> {
            DepartmentBO ret = new DepartmentBO();
            BeanUtils.copyProperties(item, ret);
            list.add(ret);
        });
        return list;
    }

    @Override
    public void addDepartment(DepartmentBO departmentBO) {
        Department department = BeanConvertor.transfer(departmentBO, Department::new);
        deptRepository.save(department);
    }
}
